package com.epam.cdp.jpa.backingbeans;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.epam.cdp.jpa.dao.UserDAO;
import com.epam.cdp.jpa.model.Role;
import com.epam.cdp.jpa.model.User;

@ManagedBean
@SessionScoped
public class UsersAdministrationBackingBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private final static Log log = LogFactory.getLog(UsersAdministrationBackingBean.class);
    
    @EJB
    private transient UserDAO userDAO;
    
    private User newUser = new User();
    private User selectedUser = new User();
    
    public List<User> getAllUsers() {
	List<User> allUsers = userDAO.getAll();
	return allUsers;
    }

    public void removeUser(long id) {
	userDAO.remove(id);
    }

    public void addNewUser() {
	userDAO.persist(newUser);
	newUser = new User();
    }
    
    public void updateSelectedUser() {
	userDAO.update(selectedUser);
	selectedUser = new User();
    }
    
    public List<Role> getAllUsersRole() {
	return Arrays.asList(Role.values());
    }
    
    public User getNewUser() {
        return newUser;
    }

    public void setNewUser(User newUser) {
        this.newUser = newUser;
    }

    public User getSelectedUser() {
        return selectedUser;
    }

    public void selectUser(User selectedUser) {
	log.info("selected " + selectedUser);
        this.selectedUser = selectedUser;
    }
    
}
